package com.yiwei.jmx.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.jmx.R;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.api.bean.LoginBean;
import com.yiwei.jmx.api.bean.RegisterBean;
import com.yiwei.jmx.base.BaseActivity;
import com.yiwei.jmx.util.Dialog;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.util.UI;
import com.yiwei.jmx.util.Util;
import com.yiwei.jmx.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class RegisterActivity extends BaseActivity {

    @BindView(R.id.txt_title)
    TextView txtTitle;

    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.btn_wechat)
    ImageView btnWechat;
    @BindView(R.id.edit_pass)
    EditText editPass;
    @BindView(R.id.btn_pass_clear)
    ImageView btnPassClear;
    @BindView(R.id.btn_pass_open)
    ImageView btnPassOpen;
    @BindView(R.id.edit_repass)
    EditText editRepass;
    @BindView(R.id.btn_repass_clear)
    ImageView btnRepassClear;
    @BindView(R.id.btn_repass_open)
    ImageView btnRepassOpen;
    @BindView(R.id.edit_email)
    EditText editEmail;
    @BindView(R.id.btn_email_clear)
    ImageView btnEmailClear;
    @BindView(R.id.edit_invite_code)
    EditText editInviteCode;
    @BindView(R.id.btn_agree)
    ImageView btnAgree;
    @BindView(R.id.txt_agree)
    TextView txtAgree;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;

    private Unbinder unbiner;
    private boolean isCheckedRepass;
    private boolean isCheckedPass;
    private boolean isAgree;
    private RegisterBean registerBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initBar();
        setBarClick();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("注册");
        registerBean = new RegisterBean();
        String ctoken = getIntent().getStringExtra("ctoken");
        String account = getIntent().getStringExtra("account");
        registerBean.ctoken = ctoken;
        registerBean.account = account;
    }

    @OnTextChanged({R.id.edit_pass, R.id.edit_repass, R.id.edit_email})
    void onTextChange(Editable s) {
        if (!TextUtils.isEmpty(UI.toString(editPass)) && !TextUtils.isEmpty(UI.toString(editRepass)) && !TextUtils.isEmpty(UI.toString(editEmail)) && isAgree) {
            btnSubmit.setSelected(true);
        } else {
            btnSubmit.setSelected(false);
        }
    }

    public void getRegister() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        loadingDialog.show();

        Api.api_service.getRegister(registerBean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    Util.startActivity(this, CreateGestureActivity.class);
                    ToastUtil.makeText(this, "注册成功");
                    finish();
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_pass_clear, R.id.btn_pass_open, R.id.btn_repass_clear, R.id.btn_repass_open, R.id.btn_email_clear, R.id.btn_agree, R.id.txt_agree, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_pass_clear:
                editPass.setText("");
                break;
            case R.id.btn_pass_open:
                isCheckedPass = !isCheckedPass;
                if (isCheckedPass) {
                    //如果选中，显示密码
                    editPass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    //否则隐藏密码
                    editPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
            case R.id.btn_repass_clear:
                editRepass.setText("");
                break;
            case R.id.btn_repass_open:
                isCheckedRepass = !isCheckedRepass;
                if (isCheckedRepass) {
                    //如果选中，显示密码
                    editRepass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    //否则隐藏密码
                    editRepass.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
            case R.id.btn_email_clear:
                editEmail.setText("");
                break;
            case R.id.btn_agree:
                isAgree = !isAgree;
                if (isAgree) {
                    btnAgree.setImageResource(R.mipmap.img_step_check);
                } else {
                    btnAgree.setImageResource(R.mipmap.img_step_uncheck);
                }
                break;
            case R.id.txt_agree:
                break;
            case R.id.btn_submit:
                registerBean.password = UI.toString(editPass);
                registerBean.password_confirm = UI.toString(editRepass);
                registerBean.email = UI.toString(editEmail);
                registerBean.invite_code = UI.toString(editInviteCode);
                if (getCheckEmpty()) {
                    if (TextUtils.isEmpty(UI.toString(editInviteCode))) {
                        Dialog.showDefaultDialog(this, "", "填写邀请码，好友将获得佣金奖励，您确定要跳过吗？", "跳过", "继续填写", false, new Dialog.DialogDefaultClickListener() {
                            @Override
                            public void confirm() {

                            }

                            @Override
                            public void cancel() {
                                getRegister();
                            }
                        });
                        return;
                    }

                    getRegister();
                }
                break;
        }
    }

    public boolean getCheckEmpty() {

        if (TextUtils.isEmpty(registerBean.password) || registerBean.password.length() < 6) {
            ToastUtil.makeText(this, "密码不能少于6位");
            return false;
        }
        if (TextUtils.isEmpty(registerBean.password_confirm) && registerBean.password_confirm.equals(registerBean.password)) {
            ToastUtil.makeText(this, "2次输入密码不一致");
            return false;
        }
        if (TextUtils.isEmpty(registerBean.email)) {
            ToastUtil.makeText(this, "邮箱不能为空");
            return false;
        }
        if (!isAgree) {
            ToastUtil.makeText(this, "请勾选同意协议");
            return false;
        }
        return true;
    }
}
